﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.Collections;
using System.Xml;

/// <summary>
/// Summary description for Sitemap
/// </summary>
public class Sitemap
{
    StringBuilder sb = null, sb_header = null;
    private Int32 iProceededItems = 0;
    private ArrayList arrResult = new ArrayList();
    double startCounter;

    public Sitemap()
    {
        sb = new StringBuilder();
        startCounter = Convert.ToDouble(DateTime.Now.TimeOfDay.TotalSeconds);
        String sTimeStamp = String.Format("{0}-{1}-{2}", DateTime.Now.Year, DateTime.Now.Month.ToString().PadLeft(2, '0'), DateTime.Now.Day.ToString().PadLeft(2, '0'));

        for (int iLangID = 1; iLangID < 3; iLangID++)
        {

            string slink = string.Format("http://rondodance.com/default.aspx?langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/about.aspx?langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/About.aspx?type=2&amp;langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/About.aspx?type=3&amp;langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/About.aspx?type=4&amp;langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/desk.aspx?langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/gallery.aspx?langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/VideoArchive.aspx?langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/Events.aspx?langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/Events.aspx?type=1&amp;langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            slink = string.Format("http://rondodance.com/News.aspx?langid={0}", iLangID);
            AddSingleNode(slink, sTimeStamp);

            Dal.arParams.Clear();
            Dal.arParams.Add(iLangID);
            DataSet DT = Dal.ExecuteDBspDS("Admin_GetNewsForSiteMap", Dal.arParams);
            if (DT != null)
            {
                if (DT.Tables[0].Rows.Count > 0)
                {
                    for (Int32 i = 0; i < DT.Tables[0].Rows.Count; i++)
                    {
                        DataRow row = DT.Tables[0].Rows[i];

                        slink = string.Format("http://rondodance.com/Article.aspx?id={0}", row["ID"]);
                        AddSingleNode(slink, sTimeStamp);

                        iProceededItems++;
                    }
                }
            }
        }

    }

    public void Save()
    {
        if (sb.Length > 0)
        {
            try
            {
                RSS_Header();
                RSS_Footer();

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(sb.ToString());
                string sSiteMapPath = HttpContext.Current.Server.MapPath("~/siteMap/");
                xDoc.Save(String.Format(@"{0}SiteMap.xml", sSiteMapPath));

                double endCounter = Convert.ToDouble(DateTime.Now.TimeOfDay.TotalSeconds);
                double totalSeconds = endCounter - startCounter;
                arrResult.Add(String.Format("Sitemap : proceeded of {0} items", iProceededItems));
            }
            catch { }
        }
    }

    private void AddSingleNode(string sLink, string sTimeStamp)
    {
        sb.Append(String.Format("<url>"));
        sb.Append(String.Format("<loc>{0}</loc>", sLink));
        sb.Append(String.Format("<lastmod>{0}</lastmod>", sTimeStamp));
        sb.Append(String.Format("<mobile:mobile />"));
        sb.Append(String.Format("</url>"));
    }

    /// <summary>
    /// RSS Header
    /// </summary>
    /// <returns></returns>
    private void RSS_Header()
    {
        sb_header = new StringBuilder();
        sb_header.Append("<?xml version='1.0' encoding='UTF-8'?>");
        sb_header.Append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:mobile=\"http://www.google.com/schemas/sitemap-mobile/1.0\">");
        sb.Insert(0, sb_header.ToString());
    }

    /// <summary>
    /// RSS Footer
    /// </summary>
    /// <returns></returns>
    private void RSS_Footer()
    {
        sb.Append("</urlset>");
    }
}